package example.cdx.com.aliplayerdemo.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;

import java.lang.ref.WeakReference;

import example.cdx.com.aliplayerdemo.R;
import example.cdx.com.aliplayerdemo.constants.PlayParameter;
import example.cdx.com.aliplayerdemo.utils.VidStsUtil;
import example.cdx.com.aliplayerdemo.widget.AliyunVodPlayerView;

/**
 * 播放器Activity
 */
public class PlayerActivity extends AppCompatActivity {

    private Context mContext;//上下文
    private AliyunVodPlayerView mApvVideo;//播放器

    private boolean inRequest;//是否正在请求

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        initParamsAndValus();

        initView();

        initData();
    }

    private void initParamsAndValus() {
        mContext = this;
    }

    private void initView() {
        mApvVideo = findViewById(R.id.apv_video);
        initAliVcMediaPlayer();
    }

    private void initAliVcMediaPlayer() {
        
    }

    private void initData() {
        requestVidSts();
    }

    /**
     * 请求sts
     */
    private void requestVidSts() {
        if (inRequest) {
            return;
        }
        inRequest = true;
        PlayParameter.PLAY_PARAM_VID = PlayParameter.DEFAULT_VID;
        VidStsUtil.getVidSts(PlayParameter.PLAY_PARAM_VID, new MyStsListener(this));
    }

    private static class MyStsListener implements VidStsUtil.OnStsResultListener {

        private WeakReference<PlayerActivity> weakctivity;

        MyStsListener(PlayerActivity act) {
            weakctivity = new WeakReference<>(act);
        }

        @Override
        public void onSuccess(String vid, String akid, String akSecret, String token) {
            PlayerActivity activity = weakctivity.get();
            if (activity != null) {
                activity.onStsSuccess(vid, akid, akSecret, token);
            }
        }

        @Override
        public void onFail() {
            PlayerActivity activity = weakctivity.get();
            if (activity != null) {
                activity.onStsFail();
            }
        }
    }

    private void onStsFail() {


    }

    private void onStsSuccess(String vid, String akid, String akSecret, String token) {

    }
}
